{% load staticfiles %}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta charset="UTF-8"> 
        <link rel="stylesheet" href="{% static 'js/plugins/jqconsole/css/ansi.css' %}" media="all" />
        <script src="{% static 'js/plugins/jqconsole/js/jquery-1.5.1.js' %}"></script>
        <script src="{% static 'js/plugins/jqconsole/js/jqconsole-2.7.min.js' %}"></script>

        <style>
            html, body {
                background-color: #333;
                color: white;
                font-family: monospace;
                margin: 0;
                padding: 0;
            }
            #console {
                width: 100%;
                background-color: black;
                border: 3px solid #CCC;
            }
            .jqconsole {
                padding: 10px;
                padding-bottom: 10px;
            }
            .jqconsole-cursor {
                background-color: #999;
            }
            .jqconsole-blurred .jqconsole-cursor {
                background-color: #666;
            }
            .jqconsole-prompt {
                color: #0d0;
            }
            .jqconsole-old-prompt {
                color: #0b0;
                font-weight: normal;
            }
            .jqconsole-input {
                color: #dd0;
            }
            .jqconsole-output-error {
                color: #F00;
            }
            .jqconsole-output-warning {
                color: #FF0;
            }
            .jqconsole-output-success {
                color: #0D0;
            }
            .jqconsole-old-input {
                color: #bb0;
                font-weight: normal;
            }
            .brace {
                color: #00FFFF;
            }
            .paran {
                color: #FF00FF;
            }
            .bracket {
                color: #FFFF00;
            }
            .jqconsole-composition {
                background-color: red;
            }

            {% if error_msg %}
                .jqconsole-header {
                    color: #F00;
                }
            {% else %}
                .jqconsole-header {
                    color: #0D0;
                }
            {% endif %}
        </style>
        <script>
            // 使用 Django csrf 功能
            $.ajaxSetup({headers: {"X-CSRFToken": '{{ csrf_token }}'}});

            // 初始化全局 业务组明细状态数组
            inception_business_detail_status = new Array();
            // 初始化全局 业务组明细数组
            inception_business_details = new Array();
            {% for dbmp_inception_business_detail in dbmp_inception_business_details %}
                inception_business_detail = new Object();
                inception_business_detail.inception_business_detail_id = {{ dbmp_inception_business_detail.inception_business_detail_id }};
                inception_business_detail.mysql_business_id = {{ dbmp_inception_business_detail.mysql_business_id }};
                inception_business_detail.execute_status = {{ dbmp_inception_business_detail.execute_status }};
                inception_business_detail.db_name = "{{ dbmp_inception_business_detail.db_name }}";
                inception_business_detail.host = "{{ dbmp_inception_business_detail.host }}";
                inception_business_detail.port = {{ dbmp_inception_business_detail.port }};
                inception_business_detail.business_name = "{{ dbmp_inception_business_detail.business_name }}";
                inception_business_details.push(inception_business_detail);
            {% endfor %}

            $(function() {
                // Creating the console.
                var header = "";
                {% if error_msg %} // 如果过错误信息则显示错误信息
                    {% for msg in error_msg %}
                        header += "{{ msg }} \n";
                    {% endfor %}
                {% endif %}
                header += "执行SQL业务组信息: \n";
                header += "  业务组名: {{ dbmp_inception_business.name }}" + "\n";
                header += " \n";
                window.jqconsole = $("#console").jqconsole(header, "> ");
                
                // Handle a command.
                var handler = function(command) {
                    jqconsole.Prompt(true, handler, function(command) {
                        // Continue line if can"t compile the command.
                        try {
                            Function(command);
                        } catch (e) {
                            if (/[\[\{\(]$/.test(command)) {
                                return 1;
                            } else {
                                return 0;
                            }
                        }
                        return false;
                    });
                };
                
                // Initiate the first prompt.
                handler();
                window.jqconsole.Write("====================== 正在执行... ========================= \n", "jqconsole-output-warning");

                // 执行SQL
                var execute_status = {{ dbmp_inception_business.execute_status }};
                if (execute_status == 1 || execute_status == 3 || execute_status == 4) {
                    // 执行业务组SQL审核
                    run_inception({{ dbmp_inception_business.inception_business_id }});
                }
            });

            // 后台调用对业务组执行SQL
            function run_inception(inception_business_id) {
                // 如果数组中没有数据返回
                if(inception_business_details.length < 1) {
                    // 改变业务组状态和父窗口页面信息
                    status = 1;
                    if($.inArray(2, inception_business_detail_status) != -1 && $.inArray(3, inception_business_detail_status) == -1) { // SQL都执行成功
                        status = 2;
                        window.jqconsole.Write("SQL全部执行成功 \n", "jqconsole-output-success");
                        change_parent_business_tr(inception_business_id, "success");
                        remove_parent_business_button(inception_business_id);
                        change_parent_business_button_ok(inception_business_id);
                    } else if($.inArray(2, inception_business_detail_status) != -1 && $.inArray(3, inception_business_detail_status) != -1) { // 部分SQL执行失败
                        status = 4;
                        window.jqconsole.Write("SQL部分执行失败(有执行失败的请查看执行记录确认) \n", "jqconsole-output-warning");
                        change_parent_business_tr(inception_business_id, "warning");
                    } else if($.inArray(2, inception_business_detail_status) == -1 && $.inArray(3, inception_business_detail_status) != -1) { // SQL都执行失败
                        status = 3;
                        window.jqconsole.Write("SQL执行全部失败(具体请查看执行记录) \n", "jqconsole-output-error");
                        change_parent_business_tr(inception_business_id, "danger");
                    }
                    window.jqconsole.Write("================ 正在修改业务组状态... ================= \n", "jqconsole-output-warning");
                    change_execute_status(inception_business_id, status); // 修改业务组执行状态

                    return false; 退出递归函数
                }
                // 获得业务明细信息 构造成字符串然后显示
                inception_business_detail = inception_business_details.shift();
                detail_info = "";
                detail_info += "执行SQL业务组明细信息: \n";
                detail_info += "  业务组名: " + inception_business_detail.business_name + "\n";
                detail_info += "  数据库名: " + inception_business_detail.db_name + "\n";
                detail_info += "  IP地址: " + inception_business_detail.host + "\n";
                detail_info += "  端口: " + inception_business_detail.port + "\n";
                window.jqconsole.Write(detail_info);
                
                $.ajax({
                    url: "{% url 'dbmp_inception_business_detail_ajax_run_inception' %}",
                    type: "POST",
                    dataType: "json",
                    data: {
                        inception_business_detail_id: inception_business_detail.inception_business_detail_id
                    }
                }).done(function(data) { 
                    if(data.is_ok == true) {
                        inception_header = "ID | stage | errLevel | stagestatus | Affected_rows | sequence | backup_dbname | execute_time | sqlsha1 \n";
                        window.jqconsole.Write(inception_header);
                        for(i=0; i<data.inception_info.length; i++) {
                            // 显示执行审核返回的数据
                            inception_body = "";
                            inception_body += data.inception_info[i].ID + " | ";
                            inception_body += data.inception_info[i].stage + " | ";
                            inception_body += data.inception_info[i].errlevel + " | ";
                            inception_body += data.inception_info[i].stagestatus + " | ";
                            inception_body += data.inception_info[i].Affected_rows + " | ";
                            inception_body += data.inception_info[i].sequence + " | ";
                            inception_body += data.inception_info[i].backup_dbname + " | ";
                            inception_body += data.inception_info[i].execute_time + " | ";
                            inception_body += data.inception_info[i].sqlsha1 + " \n";
                            inception_body += "errorMessage: " + data.inception_info[i].errormessage + "\n";
                            inception_body += "SQL: " + data.inception_info[i].SQL + "\n";
                            if(data.inception_info[i].errlevel == 0) {
                                window.jqconsole.Write(inception_body, "jqconsole-output-success");
                            } else if(data.inception_info[i].errlevel == 1) {
                                window.jqconsole.Write(inception_body, "jqconsole-output-warning");
                            } else if(data.inception_info[i].errlevel == 2) {
                                window.jqconsole.Write(inception_body, "jqconsole-output-error");
                            }
                        }
                      
                        inception_business_detail_status.push(data.execute_status); // 添加业务组明细执行SQL状态
                    } else {
                        window.jqconsole.Write("  执行SQL失败" + "\n", "jqconsole-output-error");
                    }
                    window.jqconsole.Write(data.err_msg + "\n", "jqconsole-output-error");
                    window.jqconsole.Write("-----------------------------------------");
                    window.jqconsole.Write("-----------------------------------------\n");

                    run_inception(inception_business_id); // 递归执行 SQL执行
                }).error(function(data) { 
                    window.jqconsole.Write("  请求执行SQL失败" + "\n", "jqconsole-output-error");
                }); 

            }

            // 修改父窗口数据库 tr 为成功样式
            function change_parent_business_tr(inception_business_id, class_color) {
                attr = $("#inception_business_" + inception_business_id, window.parent.document).attr("class");
                $("#inception_business_" + inception_business_id, window.parent.document).removeClass(attr);
                $("#inception_business_" + inception_business_id, window.parent.document).addClass(class_color);
            }

            // 移除父窗口业务组按钮
            function remove_parent_business_button(inception_business_id) {
                $("#business_button_" + inception_business_id, window.parent.document).remove();
            }

            // 修改父窗口数据库 按钮 为成功样式
            function change_parent_business_button_ok(inception_business_id) {
                // 构造成功显示标签
                html_str = "";
                html_str += '<a id="business_button_' + inception_business_id + '">';
                html_str += '    <i class="fa fa-check text-navy"></i>';
                html_str += '    成功';
                html_str += '</a>';
                $("#inception_business_button_td_" + inception_business_id, window.parent.document).append(html_str);
            }

            // 修改业务组执行状态
            function change_execute_status(inception_business_id, execute_status) {
                $.ajax({
                    url: "{% url 'dbmp_inception_business_ajax_change_execute_status' %}",
                    type: "POST",
                    dataType: "json",
                    data: {
                        inception_business_id: inception_business_id,
                        execute_status: execute_status
                    }
                }).done(function(data) { 
                    if(data.is_ok == true) {
                        window.jqconsole.Write("业务组状态修改成功 \n", "jqconsole-output-success");
                    } else {
                        window.jqconsole.Write("业务组状态修改失败 \n", "jqconsole-output-error");
                        window.jqconsole.Write(data.err_msg + "\n", "jqconsole-output-error");
                    }
                }).error(function(data) { 
                    window.jqconsole.Write("请求修改业务业务组状态失败", "jqconsole-output-success");
                    window.jqconsole.Write(data.err_msg + "\n", "jqconsole-output-error");
                }); 
            }

        </script>
    </head>
    <body>
        <div id="console"></div>
    </body>
</html>
